package cn.mulanbay.job.persistent.service;

import cn.mulanbay.common.bean.PageSearch;
import cn.mulanbay.persistent.query.QueryBuilder;
import cn.mulanbay.schedule.domain.TaskLog;
import cn.mulanbay.schedule.domain.TaskTrigger;
import cn.mulanbay.schedule.mapper.TaskLogMapper;
import cn.mulanbay.schedule.mapper.TaskTriggerMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TaskLogService extends ServiceImpl<TaskLogMapper, TaskLog> {

    @Autowired
    TaskTriggerMapper taskTriggerMapper;

    /**
     * 获取列表
     *
     * @param us
     * @return
     */
    public Page<TaskLog> getDataList(PageSearch us){
        Page<TaskLog> pp = new Page<>(us.getPage(),us.getPageSize());
        QueryWrapper qw = QueryBuilder.build(us);
        pp.addOrder(OrderItem.desc("start_time"));
        if(us.getPage()<0){
            List<TaskLog> list = baseMapper.selectList(qw);
            pp.setRecords(list);
        }else{
            pp = baseMapper.selectPage(pp,qw);
        }
        for(TaskLog log : pp.getRecords()){
            TaskTrigger tt = taskTriggerMapper.selectById(log.getTaskTriggerId());
            log.setTaskTrigger(tt);
        }
        return pp;
    }

}
